package cn.edu.swpu.cins.u_chain.frontend.service.project.Impl;

import cn.edu.swpu.cins.u_chain.frontend.dao.project.ProjectDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.project.*;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.project.ProInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.project.ProSkillsNeedInfo;
import cn.edu.swpu.cins.u_chain.frontend.service.project.ProjectService;
import cn.edu.swpu.cins.u_chain.utils.UUIDService;
import cn.edu.swpu.cins.u_chain.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private UploadFile uploadFile;

    @Value("${u_chain.size}")
    private int size;

    @Autowired
    private UUIDService uuidService;

    /**
     * 完成操作包括 添加项目信息 技能信息  上传文件
     * 如果有需求文档首先 加上UUID标识需求文件 然后添加项目信息  然后获取ProId然后添加项目技能信息
     *
     * @param addPro
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, IOException.class, SQLException.class})
    public int addPro(AddPro addPro, int userId){
        ProInfo proInfo = new ProInfo(addPro);
        proInfo.setProPublishedDate(new Date().getTime());
        proInfo.setUserId(userId);
        projectDao.insertProInfo(proInfo);
        int proId = proInfo.getProId();
        List<ProSkillsNeedInfo> skillsNeedInfoList = addPro.getProSkillsNeedInfos();
        skillsNeedInfoList.stream().forEach(skillsNeedInfo -> {
            skillsNeedInfo.setProId(proId);
        });
        projectDao.insertProSkills(skillsNeedInfoList);
        return proId;
    }

    @Override
    public int uploadPRD(MultipartFile file, int proId) throws IOException {
        if (file != null) {
            String urlPath = uploadFile.upload(file, proId,"PRD");
            projectDao.updatePRDStatus(proId, urlPath);
        }
        return 1;
    }

    /**
     * 这个时候当然是是使用stream最方便了啊
     * @param pageNum
     * @return
     */
    @Override
    public List<ProViewDetail> getProsInfo(int pageNum) {
        int offset = --pageNum * size;
        List<ProInfoView> proInfoViewList = projectDao.getAllPros(size, offset);
        return getProViewDetails(proInfoViewList);
    }

    /**
     * 获取我发布过的项目
     * @param userId
     * @return
     */
    @Override
    public List<ProViewDetail> getProsByMe(int userId,int pageNum) {
        int offset = --pageNum * size;
        List<ProInfoView> proInfoViews = projectDao.selectProByUserId(userId, size, offset);
        return getProViewDetails(proInfoViews);
    }

    /**
     * 获取所有有关我的评价
     * @param userId
     * @param pageNum
     * @return
     */
    @Override
    public MyProAssess getMyProAssess(int userId, int pageNum) {
        int offset = --pageNum * size;
        List<ProAssessView> viewList = projectDao.selectProAssess(userId, size, offset);
        //获取我给出的评价
        List<ProAssessView> meToAssess = viewList.stream()
                .filter(proAssessView -> proAssessView.getProPubId() == userId)
                .collect(Collectors.toList());
        //获取对于我的评价
        List<ProAssessView> assessToMe = viewList.stream()
                .filter(proAssessView -> proAssessView.getProDevId() == userId)
                .collect(Collectors.toList());
        MyProAssess myProAssess = new MyProAssess(assessToMe,meToAssess);
        return myProAssess;
    }

    public List<ProViewDetail> getProViewDetails(List<ProInfoView> proInfoViews) {
        List<ProViewDetail> proViewDetails = new ArrayList<>();
        proInfoViews.stream()
                .forEach(proInfoView -> {
                    List<ProSkillNeed> skillList = projectDao.selectProSkillByProId(proInfoView.getProId());
                    ProViewDetail proViewDetail = new ProViewDetail(proInfoView,skillList);
                    proViewDetails.add(proViewDetail);
                });
        return proViewDetails;
    }



}
